package com.kcsj.service;


import com.kcsj.dao.OrderAssProductModelDao;
import com.kcsj.dao.OrderDao;
import com.kcsj.dao.TaskListDao;
import com.kcsj.dao.WayBillDao;
import com.kcsj.domain.*;
import com.kcsj.domain.user.Employee;
import com.kcsj.domain.user.Notice;
import com.kcsj.domain.user.User;
import com.kcsj.service.user.UserService;
import com.kcsj.util.Condition;
import com.kcsj.util.GenerateTaskListNo;
import com.kcsj.util.JdbcHelper;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

/**
 * @author 傅立贤
 * 29th Nov. 2020
 */
public class OrderService {
    private static OrderDao orderDao = OrderDao.getInstance();
    private static OrderService orderService =new OrderService();
    private OrderService(){}
    public static OrderService getInstance(){
        return orderService;
    }
    //Fate Grand Order?
    public boolean add(Order order) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean add = false;
        try{
            order.setOrderStatus("待发货");
            //增加订单,返回订单号
            String orderNo = orderDao.add(order, conn);
            order.setOrderNo(orderNo);
            //向OrderAssProductModel表中添加数据
            for (ProductModel productModel : order.getProductModels()) {
                OrderAssProductModel orderAssProductModel = new OrderAssProductModel();
                orderAssProductModel.setNumber(productModel.getNumber());
                orderAssProductModel.setOrder(order);
                orderAssProductModel.setProductModel(productModel);
                OrderAssProductModelDao.getInstance().add(orderAssProductModel,conn);
            }
            Employee deliveryPerson = WayBillDao.getInstance().getDeliveryMan(conn);
            //增加运单
            WayBill wayBill = new WayBill(
                    //我也不知道怎么生成了,先用着我的罢
                    GenerateTaskListNo.generate(),
                    order,
                    "未发货",
                    order.getCreateDatetime(),
                    null//预计到达时间
                    ,deliveryPerson
            );
            WayBillDao.getInstance().add(wayBill,conn);
            //增加任务单by姜瑞临
            TaskList taskList = new TaskList(
                    wayBill.getWayBillNo(),
                    order,
                    "未发货",
                    order.getCreateDatetime(),
                    wayBill.getEmployee()
            );
            User user = UserService.getInstance().find(deliveryPerson.getId());
            TaskListDao.getInstance().add(taskList,conn);
            Notice notice = new Notice("配送员","新的任务单："+wayBill.getWayBillNo(),"未读",user);
            NoticeService.getInstance().add(notice);
            conn.commit();
            add = true;
        } catch (SQLException | IOException e){
            e.printStackTrace();
            //回滚事务中所有操作
            conn.rollback();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } finally {
            //重启自动提交，事务结束
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return add;
    }

    public Order find(String orderNo) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Order order = orderDao.find(orderNo, conn);
        JdbcHelper.close(conn);
        return order;
    }

    public Collection<Order> findAll(List<Condition> conditionList) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Collection<Order> orders = orderDao.findAll(conn,conditionList);
        return orders;
    }


    public boolean update(Order order) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        boolean updated=false;
        try {
            String orderStatus = orderDao.find(order.getOrderNo(), conn).getOrderStatus();
            if(order.getOrderStatus().equals(orderStatus)){
                updated = orderDao.update(order, conn);
            }else {
                orderDao.updateOrderStatus(order,conn);
                updated =TaskListDao.getInstance().updateStatus
                        (order.getOrderNo(),order.getOrderStatus(),conn);
                WayBillDao.getInstance().updateStatus(order.getOrderNo(),order.getOrderStatus(),conn);
            }
            conn.commit();
        }catch (SQLException e){
            e.printStackTrace();
            //回滚事务中所有操作
            conn.rollback();
        }finally {
            //重启自动提交，事务结束
            conn.setAutoCommit(true);
            JdbcHelper.close(conn);
        }
        return updated;
    }
}
